package com.manong.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.manong.domain.Employee;
import com.manong.domain.Role;
import com.manong.exception.BusinessExp;
import com.manong.mapper.RoleMapper;
import com.manong.query.QueryObject;
import com.manong.query.RoleQueryObject;
import com.manong.service.IRoleService;
import com.manong.vo.RolePermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService {

    private RoleMapper roleMapper;

    @Autowired
    public void setRoleMapper(RoleMapper roleMapper) {
        this.roleMapper = roleMapper;
    }


    @Override
    @Transactional
    public void save(RolePermissionVo rolePermissionVo) {
        // 拿到role对象
        Role role = rolePermissionVo.getRole();
        // 把role存在数据库库中
        roleMapper.insert(role);
        Long[] permissionIds = rolePermissionVo.getPermissionIds();
        Long[] menuIds = rolePermissionVo.getMenuIds();
        // 维护关系-> 往中间表插入数据
        if(permissionIds != null && permissionIds.length > 0) {
            // 批量插入
            roleMapper.insertBatchRelation(role.getId(), permissionIds);
        }
        // 维护关系-> 往中间表插入数据
        if(menuIds != null && menuIds.length > 0) {
            // 批量插入
            roleMapper.insertMenuBatchRelation(role.getId(), menuIds);
        }
    }

    @Override
    public void delete(Long id) {
        if(id == null){
            throw new BusinessExp("参数不能为空");
        }
        int count = roleMapper.deleteByPrimaryKey(id);
        // count 影响行数， 如果count == 0 表示删除失败
        if(count == 0){
            throw new BusinessExp("删除失败");
        }

        // 删除关系表信息
        roleMapper.deleteRelation(id);
    }

    @Override
    public void update(RolePermissionVo rolePermissionVo) {
        // vo对象中获取role对象
        Role role = rolePermissionVo.getRole();
        roleMapper.updateByPrimaryKey(role);
        Long[] permissionIds = rolePermissionVo.getPermissionIds();
        Long[] menuIds = rolePermissionVo.getMenuIds();
        // 先删除旧关系，在维护新关系
        roleMapper.deleteRelation(role.getId());
        // 先删除旧关系，在维护新关系
        roleMapper.deleteMenuRelation(role.getId());
        // 维护关系-> 就往中间表中插入数据
        if(permissionIds != null) {
//            for(Long roleId : roleIds) {
//                // 往中间表插入数据   employee_role(employee_id, role_id)
//                employeeMapper.insertRelation(employee.getId(), roleId);
//            }
            // 批量插入
            roleMapper.insertBatchRelation(role.getId(), permissionIds);
        }
        // 维护关系-> 就往中间表中插入数据
        if(menuIds != null) {
//            for(Long roleId : roleIds) {
//                // 往中间表插入数据   employee_role(employee_id, role_id)
//                employeeMapper.insertRelation(employee.getId(), roleId);
//            }
            // 批量插入
            roleMapper.insertMenuBatchRelation(role.getId(), menuIds);
        }
    }


    @Override
    public Role get(Long id) {
        if(id == null){
            throw new BusinessExp("id不能为空");
        }
        Role role = roleMapper.selectByPrimaryKey(id);
        if(role == null){
            throw new BusinessExp("非法操作");
        }
        return role;
    }

    @Override
    public List<Role> getAll() {
        return roleMapper.selectAll();
    }

    @Override
    public PageInfo query(QueryObject qo) {
        if(qo.getPageSize() == -1){
            List<Role> roles = roleMapper.selectForList(qo);
            return new PageInfo(roles);
        } else {
            //PageHelper进行分页操作
            //告诉PageHelper 当前页是多少 每页显示数据
            PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
            //调用分页方法
            List<Role> roles = roleMapper.selectForList(qo);
            return new PageInfo(roles);
        }
    }

    @Override
    public void saveOrUpdate(RolePermissionVo rolePermissionVo) {
        if(rolePermissionVo == null){
            throw new BusinessExp("非法操作");
        }
        if(rolePermissionVo.getRole() == null){
            throw new BusinessExp("非法操作");
        }
        if(rolePermissionVo.getRole().getId() == null){
            this.save(rolePermissionVo);
        } else {
            this.update(rolePermissionVo);
        }
    }

    @Override
    public List<Role> getEmployeeRole(Long id) {
        // 1 参数校验
        if(id == null){
            throw new BusinessExp("参数不能为空");
        }
        return roleMapper.getEmployeeRoleById(id);
    }
}
